layui.use(['element', 'form', 'layer'], function () {

    var form = layui.form;
    var layer = layui.layer;
    var element = layui.element;

    var loginId_p=$.cookie('loginAdminId');//获取cookie
    if (loginId_p==null||loginId_p=='null'){//判断是否登陆 =null=没登陆
        alert("您尚未登陆,点击确定跳转至登陆页面!");
        window.location.replace("/games_info_sys/backstage_web/login.html");
    }else if(atob(loginId_p).indexOf("Logined:")==0){//登陆状态中
        //输入框特效
        $('.input-field').on('change', function () {
            var $this = $(this),
                value = $.trim($this.val()),
                $parent = $this.parent();

            if (value !== '' && !$parent.hasClass('field-focus')) {
                $parent.addClass('field-focus');
            } else {
                $parent.removeClass('field-focus');
            }
        })
        //确认修改按钮
        form.on('submit(update_btn)', function (data) {
            var aName=null;
            var newAPwd=null;
            if (utf8to16(atob(loginId_p)).substring(8,utf8to16(atob(loginId_p)).length)==$("#aName").val()){
                aName=$("#aName").val();
                var admin = {aId: null, aName: aName, aPwd:$("#aPwd").val()};
                $.ajax({
                    url: "/games_info_sys/admin/selectAdminByAdmin.do",
                    type: "POST",
                    contentType: "application/json",
                    data: JSON.stringify(admin),
                    success: function (res) {//发送成功
                        if (res.code==1){
                            if ($("#aPwd").val()==$("#newAPwd").val()){
                                layer.msg("新密码与原密码相同!请重新输入!")
                            }else {
                                newAPwd = $("#newAPwd").val();
                                var newAdmin = {aId: res.data.aId, aName: aName, aPwd: newAPwd};
                                $.ajax({
                                    url: "/games_info_sys/admin/edit.do",
                                    type: "POST",
                                    contentType: "application/json",
                                    data: JSON.stringify(newAdmin),
                                    success: function (res2) {//发送成功
                                        if (res2.data=="1"){
                                            layer.alert("修改成功!",function () {
                                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                                parent.layer.close(index); //再执行关闭
                                            })
                                        }else {
                                            layer.msg("修改失败!")
                                        }
                                    }
                                });
                            }
                        }else {
                            layer.msg("原密码输入错误!请重新输入!")
                        }
                    }
                });
            }else {
                layer.msg("用户名与当前登陆用户名不一致!请重新输入!");
            }

            return false;
        });

    }else {
        alert("您尚未登陆,点击确定跳转至登陆页面!");
        $.cookie('loginAdminId',null);
        window.location.replace("/games_info_sys/backstage_web/login.html");
    }


});
